//
//  KACustomIconViewController.m
//  KeepAccounts
//
//  Created by honeyrr on 2017/10/18.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "KACustomItemViewController.h"
#import "KACustomCollectionViewCell.h"

@interface KACustomItemViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property(nonatomic, strong) UIImageView *imageView;

@property(nonatomic, strong) UITextField *textField;

@property(nonatomic, strong) UICollectionView *collectionView;

@property(nonatomic, strong) NSArray *itemArr;

@property(nonatomic, copy) NSString *flagImgName;
@end

static NSString *const itemCustomCollectionViewCell = @"itemCustomCollectionViewCell";

@implementation KACustomItemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //otherIconBtn
    
    [self setUpUI];
}

-(NSArray *)itemArr
{
    if (!_itemArr) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"otherIconBtn.plist" ofType:nil];
        _itemArr = [NSArray arrayWithContentsOfFile:path];
    }
    return _itemArr;
}

-(void) setUpUI
{
    self.navigationItem.title = @"添加类别";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(addItemClass)];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 64 + 3, 40, 40)];
    imageView.image = [UIImage imageNamed:@"placeholder"];
    [self.view addSubview:imageView];
    _imageView = imageView;
    
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(60, 64 + 5, 100, 40)];
    textField.placeholder = @"自定义输入名称";
    [self.view addSubview:textField];
    _textField = textField;
    
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([KACustomCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:itemCustomCollectionViewCell];
    [self.view addSubview:self.collectionView];
    
}
-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.itemSize = CGSizeMake(50, 50);
        layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        UICollectionView *cv = [[UICollectionView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_textField.frame) + 3, KAScreenW, KAScreenH - 40 - 64) collectionViewLayout:layout];
        cv.backgroundColor = [UIColor groupTableViewBackgroundColor];
        cv.delegate = self;
        cv.dataSource = self;
        _collectionView = cv;
    }
    return _collectionView;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    KACustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:itemCustomCollectionViewCell forIndexPath:indexPath];
    cell.imgName = self.itemArr[indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    _imageView.image = [UIImage imageNamed:self.itemArr[indexPath.row]];
    _flagImgName = self.itemArr[indexPath.row];
}

-(void) addItemClass
{
    NSDictionary *dict = @{@"imageName":_flagImgName, @"titleName":_textField.text};
    NSString *plistPath = @"";
    if ([self.billType isEqualToString:@"支出"]) {
        plistPath = KAOutlayPlistPath;
    } else {
        plistPath = KAIncomePlistPath;
    }
    [[KAReadFile sharedInstance] writeDataToPlist:plistPath data:dict];
    [self.navigationController popViewControllerAnimated:YES];
    
}

@end
